<html>
<head><meta charset="utf-8"><title>vtable as repr(C) struct v.s. intrinsics · t-compiler · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/index.html">t-compiler</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/vtable.20as.20repr(C).20struct.20v.2Es.2E.20intrinsics.html">vtable as repr(C) struct v.s. intrinsics</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="222554913"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/vtable%20as%20repr%28C%29%20struct%20v.s.%20intrinsics/near/222554913" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Simon Sapin <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/vtable.20as.20repr(C).20struct.20v.2Es.2E.20intrinsics.html#222554913">(Jan 13 2021 at 09:33)</a>:</h4>
<p>Hi. I’m implementing <a href="https://github.com/rust-lang/rfcs/pull/2580">https://github.com/rust-lang/rfcs/pull/2580</a> which involves <code>align_of</code> and <code>size_of</code> methods on <code>DynMetadata</code>, a vtable pointer for trait objects, without a data pointer. I could add two intrinsics similar to <code>align_of_val</code> and <code>size_of_val</code>, but the only thing they’d apparently do is access fields at fixed offsets. Having a libcore-private <code>#[repr(C)]</code> struct that has the expected layout prefix of vtables seems to work and looks much simpler: <a href="https://play.rust-lang.org/?version=stable&amp;mode=debug&amp;edition=2018&amp;gist=45db553cde7c07e4322ede1f6e75f939">https://play.rust-lang.org/?version=stable&amp;mode=debug&amp;edition=2018&amp;gist=45db553cde7c07e4322ede1f6e75f939</a>. What do you think of this strategy?</p>



<a name="222564885"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/vtable%20as%20repr%28C%29%20struct%20v.s.%20intrinsics/near/222564885" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> bjorn3 <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/vtable.20as.20repr(C).20struct.20v.2Es.2E.20intrinsics.html#222564885">(Jan 13 2021 at 11:11)</a>:</h4>
<p>We also have <a href="https://github.com/rust-lang/rust/blob/9f3998b4aa9d0eea3249fdd48b8b719185673d16/library/core/src/ptr/mod.rs#L223-L234">https://github.com/rust-lang/rust/blob/9f3998b4aa9d0eea3249fdd48b8b719185673d16/library/core/src/ptr/mod.rs#L223-L234</a> which hard-codes the layout of slices. We may just as well have an equivalent for trait objects.</p>



<a name="222617931"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/vtable%20as%20repr%28C%29%20struct%20v.s.%20intrinsics/near/222617931" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Simon Sapin <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/vtable.20as.20repr(C).20struct.20v.2Es.2E.20intrinsics.html#222617931">(Jan 13 2021 at 17:40)</a>:</h4>
<p>Yes that’s what I already do in my upcoming PR for decomposing wide pointers</p>



<a name="222618020"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/vtable%20as%20repr%28C%29%20struct%20v.s.%20intrinsics/near/222618020" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Simon Sapin <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/vtable.20as.20repr(C).20struct.20v.2Es.2E.20intrinsics.html#222618020">(Jan 13 2021 at 17:40)</a>:</h4>
<p>This gives access to the vtable pointer. Here I’m asking about accessing fields of the vtable, behind that pointer</p>



<a name="222618316"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/vtable%20as%20repr%28C%29%20struct%20v.s.%20intrinsics/near/222618316" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> bjorn3 <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/vtable.20as.20repr(C).20struct.20v.2Es.2E.20intrinsics.html#222618316">(Jan 13 2021 at 17:42)</a>:</h4>
<p>A <code>#[repr(C)]</code> struct makes sense here.</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>